package com.mm.controller;

import com.mm.config.CustomProperties;
import com.mm.constant.ResponseConstant;
import com.mm.exception.ServiceException;
import com.mm.model.sys.JsonResponse;
import com.mm.service.PictureService;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 图片Controller
 *
 * @date 2018/7/30 10:38
 */
@Controller
@RequestMapping(value = "/picutre")
public class PictureController {
	private static Logger logger = LoggerFactory.getLogger(PictureController.class);
	@Autowired
	private PictureService pictureService;
	@Autowired
	private CustomProperties customProperties;
	/**
	 * 图片上传
	 */
	@ResponseBody
	@RequestMapping(value = "/upload")
	public Object upload(@RequestParam("pictureFile") MultipartFile pictureFile) {
		//获取文件全名称
		String originalFilename = pictureFile.getOriginalFilename();
		if (StringUtils.isBlank(originalFilename)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "文件名不能为空");
		}
		//去掉后缀，获取纯文件名
		String extension = FilenameUtils.getExtension(originalFilename);
		String fileName = originalFilename.replaceAll("." + extension, "").trim();
		if (StringUtils.isBlank(fileName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "文件名不能为空");
		}
		try {
			//上传图片
			return pictureService.uploadePicture(pictureFile);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("PictureController upload " + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "上传图片失败");
		}
	}

	/**
	 * 显示图片
	 */
	@ResponseBody
	@RequestMapping(value = "/show/{name}")
	public void show(@PathVariable String name, HttpServletRequest request, HttpServletResponse response) {
		//图片存放路径
		String pictureSaveDir = customProperties.getPictureSaveDir();
		File file = new File(pictureSaveDir + name);
		//图片存在
		if (file.exists()) {
			try (FileInputStream inputStream = new FileInputStream(file); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream())){
				//读图片
				int available = inputStream.available();
				byte[] data = new byte[available];
				inputStream.read(data);
				//写图片
				response.setCharacterEncoding("UTF-8");
				outputStream.write(data);
				outputStream.flush();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				logger.error("PictureController show " + e.getMessage());
			} catch (IOException e) {
				e.printStackTrace();
				logger.error("PictureController show " + e.getMessage());
			}
		} else {
			logger.error("PictureController show 图片不存在");
		}

	}

}
